package com.marvels.approve.loan.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.approve.common.dao.McamLoanApplyInfoCommonDao;
import com.marvels.approve.consum.ApproveConsumerExternalApi;
import com.marvels.approve.consum.ApproveConsumerProductApi;
import com.marvels.approve.credit.service.CreditReportService;
import com.marvels.approve.loan.dao.ApprovalResultsDao;
import com.marvels.approve.loan.dao.PushBsDataDao;
import com.marvels.approve.loan.dto.BsBankInfoEntityDTO;
import com.marvels.approve.loan.dto.BsRepayPushDTO;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.loan.dto.PaymentResultDTO;
import com.marvels.approve.loan.service.ApprovalResultsService;
import com.marvels.approve.manager.dao.ApprovalProcessDao;
import com.marvels.approve.process.dto.LoanApplyCaseDTO;
import com.marvels.approve.process.dto.McptProductFundRelationDto;
import com.marvels.approve.process.dto.ScreenshotsDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
@Service
public class ApprovalResultsServiceImpl implements ApprovalResultsService {
	@Autowired
	private ApprovalResultsDao approvalResultsDao;
	@Autowired
	private CreditReportService creditReportService;
	@Autowired
	private ApproveConsumerProductApi consumerProductApi;
	@Autowired
	private ApproveConsumerExternalApi consumerExternalApi;
	@Autowired
	private PushBsDataDao pushBsDataDao;
	@Override
	public PageInfo<BsRepayPushDTO> selectResultsList(BsRepayPushDTO bsRepayPushDTO) throws MarvelsBusinessException{
		PageHelper.startPage(bsRepayPushDTO.getPageNum(), bsRepayPushDTO.getPageSize());
		List<BsRepayPushDTO> caseList = approvalResultsDao.selectResultsList(bsRepayPushDTO);
		PageInfo<BsRepayPushDTO> pageInfo = new PageInfo<BsRepayPushDTO>(caseList);
		return pageInfo;
	}
	@Override
	public Integer updateBsFund(BsRepayPushDTO bsRepayPushDTO)throws MarvelsBusinessException {
		if (StringUtils.isEmpty(bsRepayPushDTO.getFundCode())) {
			return null;
		}
		bsRepayPushDTO.setUpdatedBy(CacheUtils.getCacheUser().getUserName());
		Integer count = approvalResultsDao.updateBsFund(bsRepayPushDTO);
		return count;
	}
	@Override
	public BsRepayPushDTO selectBsFund(BsRepayPushDTO bsRepayPushDTO)throws MarvelsBusinessException {
		BsRepayPushDTO pushDTO = new BsRepayPushDTO();
		McamLoanApplyInfoDTO userInfo = approvalResultsDao.selectApplyCase(bsRepayPushDTO.getLoanId());//得到基本信息
		McptProductFundRelationDto fundRelationDto = new McptProductFundRelationDto();
		fundRelationDto.setProductCode(userInfo.getProductCode());
		List<McptProductFundRelationDto> relationList = consumerProductApi.selectProductFundRelation(fundRelationDto);//通过产品小类得到对应的资金源集
		pushDTO.setLoanId(bsRepayPushDTO.getLoanId());
		pushDTO.setCustName(userInfo.getCustName());
		pushDTO.setCustIdCard(userInfo.getCustCardNo());
		pushDTO.setProductTypeName(userInfo.getProductTypeName());
		pushDTO.setPrincipalAmtAct(userInfo.getPrincipalAmtAct()+"");
		pushDTO.setTerm(userInfo.getApplyTerms()+"");
		pushDTO.setFundList(relationList);
		return pushDTO;
	}
	@Autowired
	private McamLoanApplyInfoCommonDao mcamLoanApplyInfoCommonDao;

	
}
